package com.time.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.time.vo.ResultVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author mjw
 * @date 2022/11/29
 */
public class SecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        ResultVO<Object> resultVO = new ResultVO<>(500, "尚未登录，请登录", null);
        writer.write(new ObjectMapper().writeValueAsString(resultVO));
        writer.flush();
        writer.close();
    }
}
